<template>
  <div>
    <el-upload
      v-show="!isShowTime"
      accept="image/png,image/gif"
      class="avatar-uploader"
      list-type="picture-card"
      :file-list="fileList"
      :limit="limit"
      :on-preview="onPreview"
      :on-remove="onRemove"
      :on-change="onChange"
      :before-upload="beforeUpload"
      :http-request="onRequest"
      :on-exceed="onExceed"
      action="#"
    >
    <span>{{$t('questionNew.uploadPictures')}}</span>
    </el-upload>
    <el-progress v-if="isShowTime" type="circle" :percentage="percentage" />
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="50%"
    >
    <i class="el-icon-error"></i>
      <img style="width:100%" :src="urlImg">
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDQgiV3KL7dttfBuPeeOuAAtMZ9UcU5SZQ',
  SecretKey: 'd07xXqtgDUVGl5FF6rPilX2KQHsEKAk4'
})
export default {
  props: {
    limit: {
      type: Number,
      default: () => 0
    }
  },

  data () {
    return {
      fileList: [],
      dialogVisible: false,
      urlImg: '',
      isShowTime: false,
      percentage: 0
    }
  },

  created () {

  },

  methods: {
    // 上传文件
    onRequest (data) {
      this.isShowTime = true
      this.id = setInterval(() => {
        if (this.percentage < 97) {
          this.percentage++
        } else {
          clearInterval(this.id)
        }
      }, 300)
      cos.putObject({
        Bucket: 'hrsass-1308960779', /* 填入您自己的存储桶，必须字段 */
        Region: 'ap-shanghai', /* 存储桶所在地域，例如ap-beijing，必须字段 */
        Key: data.file.name, /* 存储在桶里的对象键（例如1.jpg，a/b/test.txt），必须字段 */
        StorageClass: 'STANDARD',
        Body: data.file, // 上传文件对象
        onProgress: progressData => {
          // console.log(progressData)
          if (progressData.percent === 1) {
            this.percentage = 100
            clearInterval(this.id)
            setTimeout(() => {
              this.isShowTime = false
              this.percentage = 0
            }, 2000)
          }
        }
      }, (err, data) => {
        if (err) {
          this.isShowTime = false
          this.$message.error('网络错误,上传失败')
          this.fileList.splice(this.fileList.length - 1, 1)
        }
        // console.log(err || data)
        if (!err && data.statusCode === 200) {
          this.$message.success('上传成功')
          const lastindex = this.fileList.length - 1
          this.fileList[lastindex].url = 'https://' + data.Location
          this.$emit('on-success', this.fileList)
        }
      })
    },
    // 预览图片
    onPreview (file) {
      this.urlImg = file.url
      this.dialogVisible = true
    },
    // 删除照片
    onRemove (file, fileList) {
      this.fileList = fileList
    },
    // 图片上传之前的钩子函数
    beforeUpload (file) {
      const imgType = ['image/png', 'image/gif']
      if (!imgType.includes(file.type)) {
        this.$message.error('请上传正确的图片格式')
      }
      const maxSize = 3 * 1024 * 1024
      if (file.size >= maxSize) {
        this.$message.error('图片太大了')
        return false
      }
    },
    // 当文件发生改变时的钩子
    onChange (file, fileList) {
      console.log(file)
      console.log(fileList)
      console.log(file.url)
      this.$emit('imgUrl', file.url)
      this.fileList = fileList
    },
    // 文件超出个数的钩子函数
    onExceed () {
      this.$message.error('图片不能超过' + this.limit + '个')
    }
  }
}
</script>

<style lang='scss'>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    width: 100px;
    height: 60px;
    background-color: #fff;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 60px;
    line-height: 60px;
    text-align: center;
  }
  .avatar {
    width: 100%;
    height: 100%;
    display: block;
  }
  .el-upload-list--picture-card .el-upload-list__item{
    width: 100px;
    height: 60px;
  }
  .el-upload--picture-card span{
    position: absolute;
    display: block;
    width: 100px;
    height: 60px;
    line-height: 60px;
    font-size: 10px;
    color: black;
  }
</style>
